// pages/collect/collect.js
const app=getApp()
let arrVideo=[]
let isBtnVideo=true
let num=0;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        musicWidth:'120rpx',
        musicHeight:'120rpx',
        videoWidth:'120rpx',
        videoHeight:'120rpx',
        jokeWidth:'120rpx',
        jokeHeight:'120rpx',
        // title:'我喜欢的音乐',
        list:arrVideo,
        startX:0,
        startY:0,
        // stopSrc:'https://s3.bmp.ovh/imgs/2021/11/a67c923cd8edbf2a.png',
        // startSrc:'https://s3.bmp.ovh/imgs/2021/11/982f4f3e0af32e4e.png'
        
    },

    // 开始滑动
    touchStart(e){
        console.log(e)
        let dataList=[...this.data.list]
        dataList.forEach(item=>{
            if(item.isTouchMove){
                item.isTouchMove=!item.isTouchMove;
            }
            console.log(item.isTouchMove)
        })
        
        this.setData({
            list:dataList,
            startX:e.touches[0].clientX,
            startY:e.touches[0].clientY
        })
    },
    touchMove(e){
        console.log(e);
        let moveX=e.changedTouches[0].clientX;
        let moveY=e.changedTouches[0].clientY;
        let id=e.currentTarget.dataset.index;
        console.log(moveX,moveY)
        console.log(this.data.startX,this.data.startY)
        let dataList=[...this.data.list]
        let angle=this.angle({
            X:this.data.startX,
            Y:this.data.startY
        },{
            X:moveX,
            Y:moveY
        });
        dataList.forEach((item,index)=>{
            item.isTouchMove=false;
            if(angle>30){
                return 
            }
            if(id==index){
                if(moveX > this.data.startX){
                    item.isTouchMove=false;
                }else{
                    item.isTouchMove=true;
                }
            }
        })
        console.log(dataList)
        this.setData({
            list:dataList
        })
    },

    // 计算滑动角度
    angle:function(start,end){
        var _X=end.X-start.X,
            _Y=end.Y-start.Y
        return 360*Math.atan(_Y/_X)/(2*Math.PI)
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        isBtnVideo=true
    },
    delItem(e){
        let id=e.currentTarget.dataset.id;
        let dataList=[...this.data.list];
        console.log(id)

        for(let i=0;i<dataList.length;i++){
            const item=dataList[i];
            item.isTouchMove=false;
            if(item.id===id){
                dataList.splice(i,1);
                break;
            }
        }
        this.setData({
            list:dataList
        })
        console.log(id)
        wx.request({
            url:'https://wx.xxmtcds.top:2333/user/collectvideo',
            method:'POST',
            dataType:'text',
            header:{
                authorization:app.globalData.token
            },
            data:{
                id:id
            },
            success:res=>{
                console.log(res) 
                wx.showToast({
                  title: '删除成功！'
                })
            }
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
       
    },
    btnMusic:function(){
        wx.redirectTo({
          url: '/pages/collectMusic/collectMusic',
        })
        
    }, 
    btnVideo:function(){
        wx.redirectTo({
          url: '/pages/collectVideo/collectVideo',
        })
        
    }, 
    btnEmoil:function(){
        wx.redirectTo({
          url: '/pages/collectJoke/collectJoke',
        })
        this.setData({
            // title:'我喜欢的视频',
            videoWidth:'120rpx',
            videoHeight:'120rpx',
            musicWidth:'120rpx',
            musicHeight:'120rpx',
            jokeWidth:'170rpx',
            jokeHeight:'170rpx'
        })
    },
    btnPlay:function(e){
        console.log(e)
        console.log(this.data.list)
        for(let i=0;i<this.data.list.length;i++){
            if(e.target.id==this.data.list[i].id){
                this.data.list[i].isPlay=true
            }else{
                this.data.list[i].isPlay=false
            }
        }
    },
        


    /**
     * 生命周期函数--监听页面显示 
     */
    onShow: function () {

        const that=this
        let style=wx.getStorageSync('style')
        if(style!=''){
            this.setData({
                contentStyle:wx.getStorageSync('style')
            })
        }else{
            this.setData({
                contentStyle:'https://pic.imgdb.cn/item/6199ad962ab3f51d91e21dd0.jpg'
            })
        }
        wx.request({
            url:'https://wx.xxmtcds.top:2333/user/collectvideo',
            header:{
                authorization:app.globalData.token
            },
            success:res=>{
                arrVideo=res.data
                console.log(res) 
                let a=[]
                console.log(arrVideo)
                for(let i=0;i<arrVideo.length;i++){
                    let arr={
                        id:arrVideo[i].video_id,
                        url:arrVideo[i].video_url,
                        isTouchMove:false,
                        isPlay:false
                    }
                    a=that.data.list
                    if(isBtnVideo){
                        a.push(arr)
                        console.log(a)
                    }
            
                }
                isBtnVideo=false
                this.setData({
                    list:a,
                    jokeWidth:'120rpx',
                    jokeHeight:'120rpx',
                    musicWidth:'120rpx',
                    musicHeight:'120rpx',
                    videoWidth:'170rpx',
                    videoHeight:'170rpx',
                })

            }
        })
                
                
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})